//
//  TWDetailsVCModalAnimationTool.swift
//  EyepetizerImitative
//
//  Created by 田伟 on 2017/3/15.
//  Copyright © 2017年 个人. All rights reserved.
//
//

import UIKit

// 为弹出动画设定的协议
protocol AnimatorPresentedDelegate : NSObjectProtocol {
    func startRect(indexPath : IndexPath) -> CGRect             // 拿到起始位置
    func endRect(indexPath : IndexPath) -> CGRect               // 拿到终点位置
    func imageView(indexPath : IndexPath) -> UIImageView        // 占位图片
}

// 为消失动画设定的协议
protocol AnimatorDismissDelegate : NSObjectProtocol {
    func indexPathForDissView() -> IndexPath                    // 返回给我当前的索引
    func imageViewForDissView() -> UIImageView                  // 占位图片
}

class TWDetailsVCModalAnimationTool: NSObject {
    var isPresented : Bool = false
    
    var presentedDelegate : AnimatorPresentedDelegate?
    var indexPath : IndexPath?
    
    var dismissDelegate : AnimatorDismissDelegate?
}

extension TWDetailsVCModalAnimationTool : UIViewControllerTransitioningDelegate {
    // 自定义弹出动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = true
        return self
    }
    // 自定义消失的动画
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        return self
    }
}

extension TWDetailsVCModalAnimationTool : UIViewControllerAnimatedTransitioning {
    // 动画执行的时间
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.25
    }
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        isPresented ? animationForPresentedView(using: transitionContext) : animationForDismissView(using: transitionContext)
    }
    
    /// 弹出具体动画的实现
    private func animationForPresentedView(using transitionContext: UIViewControllerContextTransitioning){
        // 通过key:to取出弹出的view
        let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
        // 将弹出的view添加到containerView容器视图上
        transitionContext.containerView.addSubview(presentedView)
        
        guard let presentedDelegate = presentedDelegate, let indexPath = indexPath else {
            return
        }
        // 拿到执行动画的imageView
        let startRect = presentedDelegate.startRect(indexPath: indexPath)
        let imageView = presentedDelegate.imageView(indexPath: indexPath)
        imageView.frame = startRect
        transitionContext.containerView.addSubview(imageView)                           // 添加到父控件上去
        let endRect = presentedDelegate.endRect(indexPath: indexPath)
        
        presentedView.transform = CGAffineTransform(scaleX: 1, y: 0)
        /**
         默认是0.5，0.5
         0，0 从中心原点向右下延展
         0,0.5 x从X轴的中间点，y向2边延展
         0.5,0 x周从0点，y从y轴的中间点
         */
        presentedView.layer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        presentedView.alpha = 0
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            presentedView.alpha = 1
            imageView.frame = endRect
            presentedView.transform = CGAffineTransform.identity
        }) { (_) in
            imageView.removeFromSuperview()
            // ⭐️告诉上下文转场动画结束
            transitionContext.completeTransition(true)
        }
    }
    /// 消失具体动画的实现
    private func animationForDismissView(using transitionContext: UIViewControllerContextTransitioning){
//        guard let presentedDelegate = presentedDelegate, let dismissDelegate = dismissDelegate else {
//            return
//        }
        
        // 通过key:from取出消失的view
        let dismissView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
//        dismissView.removeFromSuperview()
        dismissView.alpha = 1
        
//        let indexPath = dismissDelegate.indexPathForDissView()
//        // 获取执行动画的imageView
//        let imageView = dismissDelegate.imageViewForDissView()
//        transitionContext.containerView.addSubview(imageView)
        
        // 执行动画
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
//            imageView.frame = presentedDelegate.startRect(indexPath: indexPath)
            dismissView.alpha = 0
        }) { (_) in
//            imageView.removeFromSuperview()
            dismissView.removeFromSuperview()
            // ⭐️告诉上下文转场动画结束
            transitionContext.completeTransition(true)
        }
    }
}
